﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.AcmsControls;

public partial class Templates_GalleryItem_Display : TemplateUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public override void DataBind()
    {
        base.DataBind();
        FileId = null;
        if(AcmsContext.CurrentPost != null)
        {
            var imageControl = AcmsContext.CurrentPost.FindPostControl("Image");
            if(imageControl != null)
            {
                var imageProperties = imageControl.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                if(imageProperties != null && imageProperties.HasResource())
                {
                    FileId = imageProperties.ResourceId;
                }
            }
        }
        else
        {
            var index = ((IDataItemContainer)this.NamingContainer.NamingContainer).DisplayIndex;
            FileId = ((index % 6) + 1) * -1;
        }
    }

    public int? FileId { get; set; }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        FileId = ViewState["FileId"] as int?;
    }

    protected override object SaveViewState()
    {
        ViewState["FileId"] = FileId;
        return base.SaveViewState();
    }

    protected override void OnPreRender(EventArgs e)
    {
        if (FileId.HasValue)
        {
            Page.ClientScript.RegisterExpandoAttribute(pnlGalleryItem.ClientID, "imagePath", Page.ResolveUrl(string.Format("~/File.aspx?Id={0}&Width=625&Height=424", FileId.Value)));
        }
        base.OnPreRender(e);
    }
}
